﻿using DSInternals.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Security.AccessControl;

namespace DSInternals.DataStore.Test
{
    [TestClass]
    public class SecurityDescriptorResolverTester
    {
        [TestMethod]
        public void SecurityDescriptorResolver_ComputeHash_RootSD()
        {
            byte[] binarySecurityDescriptor = "0f00000001000480300000003c000000000000001400000002001c000100000000031400ffffffff010100000000000100000000010100000000000100000000010100000000000100000000".HexToBinary();
            byte[] expectedHash = "5763e6665429964b143bb464463bf068".HexToBinary();

            byte[] calculatedHash = SecurityDescriptorRersolver.ComputeHash(binarySecurityDescriptor);
            Assert.AreEqual(true, expectedHash.SequenceEqual(calculatedHash));
        }

        [TestMethod]
        public void SecurityDescriptorResolver_ComputeHash_BinarySD()
        {
            byte[] binarySecurityDescriptor = Convert.FromBase64String("AQAUjLAFAADMBQAAFAAAAIwAAAAEAHgAAgAAAAdaOAAgAAAAAwAAAL47DvPwn9ERtgMAAPgDZ8Glepa/5g3QEaKFAKoAMEniAQEAAAAAAAEAAAAAB1o4ACAAAAADAAAAvzsO8/Cf0RG2AwAA+ANnwaV6lr/mDdARooUAqgAwSeIBAQAAAAAAAQAAAAAEACQFHQAAAAAAJAD/AQ8AAQUAAAAAAAUVAAAAG4aQvSjWui/FV5PgAAIAAAAAJAD/AQ8AAQUAAAAAAAUVAAAAG4aQvSjWui/FV5PgNwgAAAAAFACUAAIAAQEAAAAAAAEAAAAAAAAUAP8BDwABAQAAAAAABQkAAAAAABQA/wEPAAEBAAAAAAAFEgAAAAASFAB/AQ8AAQEAAAAAAAUJAAAAABIkAH8BDwABBQAAAAAABRUAAAAbhpC9KNa6L8VXk+BOBAAABRo8ABAAAAADAAAAAEIWTMAg0BGnaACqAG4FKRTMKEg3FLxFmwetbwFeXygBAgAAAAAABSAAAAAqAgAABRo8ABAAAAADAAAAAEIWTMAg0BGnaACqAG4FKbp6lr/mDdARooUAqgAwSeIBAgAAAAAABSAAAAAqAgAABRo8ABAAAAADAA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FEygAMAAAAAEAAADlw3g/mve9RqC4nRgRbdx5AQEAAAAAAAUKAAAABRIoADABAAABAAAA3kfmkW/ZcEuVV9Y/9PPM2AEBAAAAAAAFCgAAAAASJAD/AQ8AAQUAAAAAAAUVAAAAG4aQvSjWui/FV5PgBwIAAAASGAAEAAAAAQIAAAAAAAUgAAAAKgIAAAASGAC9AQ8AAQIAAAAAAAUgAAAAIAIAAAEFAAAAAAAFFQAAABuGkL0o1rovxVeT4DcIAAABBQAAAAAABRUAAAAbhpC9KNa6L8VXk+ADAgAA");
            byte[] expectedHash = Convert.FromBase64String("2FUDMNHg430Z3T4G9yu6Pg==");

            byte[] calculatedHash = SecurityDescriptorRersolver.ComputeHash(binarySecurityDescriptor);
            Assert.AreEqual(true, expectedHash.SequenceEqual(calculatedHash));
        }

        [TestMethod]
        public void SecurityDescriptorResolver_ComputeHash_ObjectSD()
        {
            byte[] binarySecurityDescriptor = Convert.FromBase64String("AQAUjLAFAADMBQAAFAAAAIwAAAAEAHgAAgAAAAdaOAAgAAAAAwAAAL47DvPwn9ERtgMAAPgDZ8Glepa/5g3QEaKFAKoAMEniAQEAAAAAAAEAAAAAB1o4ACAAAAADAAAAvzsO8/Cf0RG2AwAA+ANnwaV6lr/mDdARooUAqgAwSeIBAQAAAAAAAQAAAAAEACQFHQAAAAAAJAD/AQ8AAQUAAAAAAAUVAAAAG4aQvSjWui/FV5PgAAIAAAAAJAD/AQ8AAQUAAAAAAAUVAAAAG4aQvSjWui/FV5PgNwgAAAAAFACUAAIAAQEAAAAAAAEAAAAAAAAUAP8BDwABAQAAAAAABQkAAAAAABQA/wEPAAEBAAAAAAAFEgAAAAASFAB/AQ8AAQEAAAAAAAUJAAAAABIkAH8BDwABBQAAAAAABRUAAAAbhpC9KNa6L8VXk+BOBAAABRo8ABAAAAADAAAAAEIWTMAg0BGnaACqAG4FKRTMKEg3FLxFmwetbwFeXygBAgAAAAAABSAAAAAqAgAABRo8ABAAAAADAAAAAEIWTMAg0BGnaACq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qAgAABRosAJQAAgACAAAAunqWv+YN0BGihQCqADBJ4gECAAAAAAAFIAAAACoCAAAFEygAMAAAAAEAAADlw3g/mve9RqC4nRgRbdx5AQEAAAAAAAUKAAAABRIoADABAAABAAAA3kfmkW/ZcEuVV9Y/9PPM2AEBAAAAAAAFCgAAAAASJAD/AQ8AAQUAAAAAAAUVAAAAG4aQvSjWui/FV5PgBwIAAAASGAAEAAAAAQIAAAAAAAUgAAAAKgIAAAASGAC9AQ8AAQIAAAAAAAUgAAAAIAIAAAEFAAAAAAAFFQAAABuGkL0o1rovxVeT4DcIAAABBQAAAAAABRUAAAAbhpC9KNa6L8VXk+ADAgAA");
            byte[] expectedHash = Convert.FromBase64String("2FUDMNHg430Z3T4G9yu6Pg==");

            var securityDescriptor = new RawSecurityDescriptor(binarySecurityDescriptor, 0);
            byte[] calculatedHash = SecurityDescriptorRersolver.ComputeHash(securityDescriptor);
            Assert.AreEqual(true, expectedHash.SequenceEqual(calculatedHash));
        }
    }
}
